package com.designoutput.ui.options
{
	import com.designoutput.elements.TextElement;
	import com.designoutput.enum.TextHAlign;
	import com.designoutput.event.ColorPadEvent;
	import com.designoutput.event.SelectEvent;
	import com.designoutput.event.SlideControlEvent;
	import com.designoutput.manager.FontManager;
	import com.designoutput.manager.ResourceManager;
	import com.designoutput.manager.fontManagerClasses.FontInfo;
	import com.designoutput.ui.controls.Checkbox;
	import com.designoutput.ui.controls.ColorPad;
	import com.designoutput.ui.controls.Dropdown;
	import com.designoutput.ui.controls.Dropslider;
	import com.designoutput.ui.controls.Literal;
	import com.designoutput.ui.controls.WriteAbleDropdown;
	import com.designoutput.ui.options.components.BaseFotmatGroup;
	import com.designoutput.ui.options.components.TextHAlignGroup;
	import com.designoutput.ui.options.components.TextVAlignGroup;
	import com.designoutput.ui.util.ColorPicker;
	import com.designoutput.utils.Globals;
	import com.designoutput.vo.TextElementFormatVo;
	
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.registerClassAlias;
	import flash.text.Font;
	import flash.utils.getDefinitionByName;
	
	import ru.etcs.utils.FontLoader;
	
	public class TextOption extends BaseOption
	{
		public var fontSizeDropDown:WriteAbleDropdown;//Dropdown;
		public var fontTypesDropDown:Dropdown;
		public var kerningCheckBox:Checkbox;
		public var leadingSizeDropDown:WriteAbleDropdown;//NumberInput; //Dropdown
		public var kerningSizeDropDown:Dropdown;
		public var kerningSizeDropSlider:Dropslider;//NumberInput;
		public var fontAlphaDropSlider:Dropslider;
		public var fontColorPad:ColorPad;
		
		private var fotmatGroup:BaseFotmatGroup;
		private var hAlignGroup:TextHAlignGroup;
		private var vAlignGroup:TextVAlignGroup;
		private var colorSizeGroup:Sprite;
		private var leadingGroup:Sprite;
		private var alphaGroup:Sprite;
		private var kerningGroup:Sprite;
		
		public var bold:Boolean;
		public var italic:Boolean;
		public var underline:Boolean;
		public var shadow:Boolean;
		public var align:String;
		public var valign:String;
		public var size:Number;
		public var color:Number = 0;
		public var fontType:String;
		public var lineAlpha:Number;
		public var kerning:Boolean = true;
		public var leading:Number = 0;
		public var letterSpacing:Number = 0;
		
		
		public function TextOption(parent:DesignoutputCard)
		{
			super(parent);
			
			fotmatGroup= new BaseFotmatGroup(this);
			fotmatGroup.x = 0;
			addChild(fotmatGroup);
			
			/**************************font color and size*******************************/
			colorSizeGroup = new Sprite();
			colorSizeGroup.x = 0;
			
			fontColorPad = new ColorPad(0,true);
			fontColorPad.addEventListener(MouseEvent.MOUSE_DOWN, this.fontColor_mouseDownHandler);
			fontColorPad.x = 10;
			fontColorPad.y = 13;
			colorSizeGroup.addChild(fontColorPad);
			
			fontSizeDropDown = new WriteAbleDropdown(50,50);
			fontSizeDropDown.addEventListener(SelectEvent.COMMAND, this.fontSize_changeHandler);
			fontSizeDropDown.x = 55;
			fontSizeDropDown.y = 13;
			colorSizeGroup.addChild(fontSizeDropDown);
			
			/************************** kerning and leading*******************************/
			kerningGroup = new Sprite;
			kerningGroup.x = 0;
			
			var lis:Literal = new Literal(ResourceManager.getValue("rm_text_kerning"));
			lis.x = 0;
			lis.y = 8;
			kerningGroup.addChild(lis);
			
			kerningSizeDropSlider = new Dropslider(50, -16, 200, 0);
			kerningSizeDropSlider.addEventListener(SlideControlEvent.DRAG, this.kerningSizeInputChange);
			kerningSizeDropSlider.x = 55;
			kerningSizeDropSlider.y = 10;
			kerningGroup.addChild(this.kerningSizeDropSlider);
			
			/*-------------------------------------*/
			leadingGroup = new Sprite;
			leadingGroup.x = 0;
			
			var li:Literal = new Literal(ResourceManager.getValue("rm_text_leading"));
			li.x = 0;
			li.y = 8;
			leadingGroup.addChild(li);
			
			leadingSizeDropDown = new WriteAbleDropdown(50, 50);
			leadingSizeDropDown.addEventListener(SelectEvent.COMMAND, leadingSizeInputChange);
			leadingSizeDropDown.x = 55;
			leadingSizeDropDown.y = 10;
			leadingGroup.addChild(leadingSizeDropDown);
			
			/************************** font alpha *******************************/
			alphaGroup = new Sprite;
			alphaGroup.x = 0;
			
			var lit:Literal=new Literal(ResourceManager.getValue("rm_font_alpha"));
			lit.x=0;
			lit.y=30;
			alphaGroup.addChild(lit);
			
			fontAlphaDropSlider=new Dropslider(50, 0, 100, 100, 88);
			fontAlphaDropSlider.addEventListener(SlideControlEvent.DRAG, this.fontAlphaChange);
			fontAlphaDropSlider.x=55;
			fontAlphaDropSlider.y=30;
			alphaGroup.addChild(fontAlphaDropSlider);
			
			/*********************************************************/
			
			hAlignGroup = new TextHAlignGroup(this);
			hAlignGroup.x = 0;
			addChild(hAlignGroup);
			
			/*********************************************************/
			vAlignGroup = new TextVAlignGroup(this);
			vAlignGroup.x = 15;
			addChild(vAlignGroup);
			
			/*********************************************************/
			
			fontTypesDropDown = new Dropdown(120,120);
			fontTypesDropDown.addEventListener(SelectEvent.COMMAND, this.fontTypesChange);
			fontTypesDropDown.x = 0;
			Globals.fontManager.onFontListReady(initFontTypeDropDown);
			
			/*********************************************************/
			depthManager.x = 0;
			addChild(depthManager);
			
			// change the order
			addChild(fontTypesDropDown);
			addChild(alphaGroup);
			addChild(leadingGroup);
			addChild(kerningGroup);
			addChild(colorSizeGroup);
			
			initFontSize();
			initLeadingInput();
			
			resize();
		}
		
		private function resize():void{
			fotmatGroup.y = 107;
			colorSizeGroup.y = 132;
			kerningGroup.y = 160;
			leadingGroup.y = 185;
			alphaGroup.y = 190;
			hAlignGroup.y = 237;
			vAlignGroup.y = 272;
			fontTypesDropDown.y = 315;
			depthManager.y = 337;
		}
		
		private function initFontSize():void{
			var arrSizes:Array = [4, 6, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72];
			var len:int = arrSizes.length;
			for(var i:int = 0; i<len;i++){
				this.fontSizeDropDown.addItem(arrSizes[i].toString(),arrSizes[i].toString(),arrSizes[i].toString()=="12");
			}
			this.fontSizeDropDown.bind();
		}
		
		private function initLeadingInput():void{
			var arrSizes:Array = [4, 6, 8, 9, 10, 11, 12, 14, 16, 18, 20, 22, 24, 26, 28, 36, 48, 72];
			var len:int = arrSizes.length;
			for(var i:int = 0; i<len;i++){
				this.leadingSizeDropDown.addItem(arrSizes[i].toString(),arrSizes[i].toString(),arrSizes[i].toString()=="12");
			}
			this.leadingSizeDropDown.bind();
		}
		
		private function initFontTypeDropDown():void{
			var availableFonts:Vector.<FontInfo> = Globals.fontManager.availableFonts;
			
			for(var i:int = 0; i < availableFonts.length; i++){
				fontTypesDropDown.addItem(availableFonts[i].name, availableFonts[i].name, i==0);
			}
			fontTypesDropDown.bind();
			
			fontType = fontTypesDropDown.value;
		}
		
		
		private function fontColor_mouseDownHandler(evt:MouseEvent):void{
			new ColorPicker(this.designOutputCard, evt.target as ColorPad).show(true, true);
			fontColorPad.addEventListener(ColorPadEvent.CHOOSE_COLOR, this.fontColor_ChangeHandler);
		}
		
		
		/**
		 * Set up the options value for text element
		 * @param element
		 * 
		 */		
		public function updateOptions(element:TextElement,bothShirtAndSleeves:Boolean=false,forSleeveElements:Boolean=false):void{
			super.updateBaseInfor(element,bothShirtAndSleeves,forSleeveElements);
			
			bold = fotmatGroup.fontBold = element.bold;
			italic = fotmatGroup.fontItalic = element.italic;
			underline = fotmatGroup.fontUnderline = element.underline;
			shadow = fotmatGroup.fontShadow = element.shadow;
			
			align = hAlignGroup.textHAlign = element.align;
			valign = vAlignGroup.textVAlign =  element.vAlign;
			
			size = element.size;
			fontSizeDropDown.Set(size.toString());
			
			color = element.fillColor;
			fontColorPad.Set(color);
			
			lineAlpha = element.fillAlpha;
			fontAlphaDropSlider.setValue(lineAlpha*100);
			
			fontType = element.font;
			fontTypesDropDown.Set(fontType);
			
			
			var letterSpacing:Number = element.letterSpacing;
			if(letterSpacing==-107374182.4){
				letterSpacing = 0;
			}
			if(!isNaN(this.letterSpacing)){
				this.letterSpacing = letterSpacing;
			}else{
				this.letterSpacing = 0;
			}
			this.kerningSizeDropSlider.setValue(this.letterSpacing);
			
			
			var leading:Number = Number(element.leading);
			if(!isNaN(this.leading)){
				this.leading = leading;
			}else{
				this.leading = 0;
			}
			
			this.leadingSizeDropDown.Set(this.leading.toString());
			
		}
		
		private function fontColor_ChangeHandler(evt:ColorPadEvent):void{
			this.color =  Number(this.fontColorPad.value);
			updateTextElemet();
		}
		
		private function fontSize_changeHandler(evt:SelectEvent):void{
			var val:Number = new Number(evt.val);
			if(!isNaN(val)){
				this.size = val;
				updateTextElemet();	
			}
		}
		
		private function fontTypesChange(evt:SelectEvent):void{
			this.fontType = fontTypesDropDown.value;
			updateTextElemet();
		}
		
		private function fontAlphaChange(evt:SlideControlEvent):void{
			var val:Number=new Number(evt.val);
			if (!isNaN(val))
			{
				this.lineAlpha = val * 0.01;
				updateTextElemet();	
			}
		}
		
		private function kerningSizeInputChange(evt:SlideControlEvent):void{
			var val:Number = new Number(evt.val);
			if(!isNaN(val)){
				this.letterSpacing = val;
				updateTextElemet();
			}
		}
		
		private function leadingSizeInputChange(evt:SelectEvent):void{
			var val:Number = new Number(evt.val);
			if(!isNaN(val)){
				this.leading = val;
				updateTextElemet();	
			}
		}
		
		public function updateTextElemet():void{
			var returnTFObj:TextElementFormatVo = new TextElementFormatVo();
			
			returnTFObj.bold = bold;
			returnTFObj.italic = italic;
			returnTFObj.underline = underline;
			returnTFObj.shadow = shadow;
			returnTFObj.align = align || TextHAlign.LEFT;
			returnTFObj.vAlign = valign;
			returnTFObj.size = size;
			returnTFObj.font = fontType;
						
			returnTFObj.leading = leading;
			returnTFObj.kerning = kerning;
			returnTFObj.letterSpacing = letterSpacing;
			
			returnTFObj.fillColor = color;
			returnTFObj.fillAlpha = lineAlpha;

			this.designOutputCard.command.setTextFormat(returnTFObj);
		}
	}
}